package com.brillilab.starter.controller.ios.lab;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.entity.PageVo;
import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.enums.lab.LabUserCardEnum;
import com.brillilab.domain.pa.lab.LabUserCardPa;
import com.brillilab.domain.po.lab.LabUserCard;
import com.brillilab.domain.po.user.Users;
import com.brillilab.domain.vo.lab.LabUserCardVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.lab.ILabUserCardService;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.entity.req.lab.BindCardReq;
import com.brillilab.starter.handler.RequestPower;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * 实验室用户磁卡API
 *
 * @author lj
 */
@RestController("IosLabUserCardController")
@RequestMapping("/ios/labUserCard")
public class LabUserCardController {
    @Autowired
    private ILabUserCardService labUserCardService;
    @Autowired
    private UsersLogic usersLogic;

    /**
     * 分页获取实验室用户磁卡
     *
     * @param cardPa
     * @return
     */
    @PostMapping("/page")
    @RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo getPage(@RequestBody LabUserCardPa cardPa,@RequestHeader(name=StarterConstant.TOKEN) String token) {
        Assert.isTrue(cardPa != null && !StringUtils.isBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        cardPa.setLabId(userInfo.getLabId());

        PageVo<LabUserCardVo> resultVo=labUserCardService.selectPage(cardPa);
        return ResponseVo.success(resultVo);
    }

    /**
     * 实验室磁卡授权
     * {\"labId\":\"实验室ID必填L\",\"cardNo\":\"磁卡号必填S\"}
     *
     * @return
     */
    @AspectLog
    @PostMapping("/empower")
    @RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo empowerCard(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody JSONObject json) {

        String cardNo=json.getString("cardNo");

        Assert.isTrue(!StringUtils.isBlank(cardNo) && !StringUtils.isBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        LabUserCard card=labUserCardService.selectCard(userInfo.getLabId(),cardNo);
        if(card != null && LabUserCardEnum.State.TIED_CARD.getValue().equals(card.getState())){
            Assert.isTrue(userInfo.getLabId().equals(card.getLabId()),"该磁卡非本实验室磁卡！");
            //如果已绑定，显示绑定者名字
            Users user=usersLogic.getUser(card.getUserId());
            return ResponseVo.failure(ResultEnum.DATA_ALREADY_EXIST,null,user.getName());
        }

        labUserCardService.empowerCard(userInfo.getLabId(),cardNo,userInfo.getName());
        return ResponseVo.success("恭喜，已成功生成授权卡！请取走磁卡！");
    }

    /**
     * 用户磁卡绑定/解绑
     *
     * @param bindPa
     * @return
     */
    @AspectLog
    @PostMapping("/bind")
    @RequestPower(LabMemberPowerEnum.user_manage)
    public ResponseVo bindCard(@RequestBody BindCardReq bindPa,@RequestHeader(name=StarterConstant.TOKEN) String token) {

        Assert.isTrue(!StringUtils.isBlank(token) || !StringUtils.isBlank(bindPa.getCardNo()) && bindPa.getUserId() != null,"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        LabUserCard card=labUserCardService.selectCard(userInfo.getLabId(),bindPa.getCardNo());
        Assert.notNull(card,"读卡失败！");
        Assert.isTrue(userInfo.getLabId().equals(card.getLabId()),"该磁卡非本实验室磁卡！");

        LabUserCard userCard=null;
        if(bindPa.getType() == 1){
            //如果已绑定，显示绑定者名字
            if(LabUserCardEnum.State.TIED_CARD.getValue().equals(card.getState())){
                Users user=usersLogic.getUser(card.getUserId());
                return ResponseVo.failure(ResultEnum.DATA_ALREADY_EXIST,null,user.getName());
            }

            userCard=labUserCardService.bindCard(userInfo.getLabId(),bindPa.getCardNo(),bindPa.getUserId());
        } else if(bindPa.getType() == 0){
            userCard=labUserCardService.unbindCard(userInfo.getLabId(),bindPa.getCardNo(),bindPa.getUserId());
        }

        Assert.notNull(userCard,"业务操作失败！");

        return ResponseVo.success("恭喜，已成功绑定磁卡！");
    }

}
